using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace modul2
{
    public partial class modul3 : Form
    {
        public modul3()
        {
            InitializeComponent();
        }
        private SysIndex MojeIndexy ;
        private SysConstraint MojeConst;
        private void modul3_Load(object sender, EventArgs e)
        {
            MojeIndexy = new SysIndex("pokus");
            MojeConst = new SysConstraint("pokuscon", MojeIndexy);
            vypis_index();
            vypis_constrain();
        }

        private void CreateIbtn_Click(object sender, EventArgs e)
        {
            string iname = this.INametb.Text;
            if (MojeIndexy.FindIndex(iname) == -1)
            {
                string itype = this.Itypecmb.Text;
                bool iunique = this.IUniquechb.Checked;
                char[] delimoters ={','};
                string itable = this.ItableTb.Text;
                string[] icols = this.IColsRB.Text.Split(delimoters);
                MojeIndexy.CreateIndex(iname, itype, iunique, itable, icols);
            }
            vypis_index();
        }

        private void DropIbtn_Click(object sender, EventArgs e)
        {
            string iname = this.INametb.Text;
            SysIndex.Index pomi = (SysIndex.Index)MojeIndexy.Columns[MojeIndexy.FindIndex(iname)];
            string[] ppolia = new string[pomi.Columns.Count];
            Array.Copy(pomi.Columns.ToArray(), ppolia, pomi.Columns.Count);
            if ((MojeConst.FindConCols("PK", pomi.Table, ppolia) == -1) && (MojeConst.FindConCols("FK", pomi.Table, ppolia) == -1))
            {
                MojeIndexy.DropIndex(iname);
            }
            else {
                this.istatusrb.Text = "There is a constraint on this index";
            }
            vypis_index();
        }

        private void vypis_index()
        {
            lindexlb.Items.Clear();
            foreach (SysIndex.Index pom in MojeIndexy.Columns) {
                lindexlb.Items.Add(pom.Name);
            }
        }

        private void vypis_constrain() {
           this.conlb.Items.Clear();
            foreach(SysConstraint.Constrain pom in this.MojeConst.Constrains){
                conlb.Items.Add(pom.Name);
            }
        }
        private void ListIndexbtn_Click(object sender, EventArgs e)
        {
            istatusrb.Clear();
            string iname = this.INametb.Text;
            istatusrb.Text += MojeIndexy.ListIndex(iname);
        }

        private void Isinindexbtn_Click(object sender, EventArgs e)
        {
            istatusrb.Clear();
            string iname = this.INametb.Text;
            if(MojeIndexy.IsInIndex(iname)){
                istatusrb.Text="Column has Index";
            }else{
                istatusrb.Text="Column has not Index";
            }
        }

        private void lindexlb_DoubleClick(object sender, EventArgs e)
        {
            if (this.lindexlb.SelectedIndex != -1)
            {
                string iname = this.lindexlb.SelectedItem.ToString();
                int pos = MojeIndexy.FindIndex(iname);
                if (pos != -1)
                {
                    SysIndex.Index index = MojeIndexy[pos];
                    this.INametb.Text = index.Name;
                    this.Itypecmb.Text = index.Type;
                    this.IUniquechb.Checked = index.Unique;
                    this.ItableTb.Text = index.Table;
                    IColsRB.Clear();
                    for (int i = 0; i < index.Columns.Count; i++)
                    {
                        string s = (string)index.Columns[i];
                        IColsRB.Text += s;
                        if (i != index.Columns.Count - 1)
                        {
                            IColsRB.Text += ",";
                        }
                    }
                    IColsRB.Text.TrimEnd(',');
                }
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label15_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string cname = this.CnameTb.Text;
            this.CstatusRB.Clear();
            this.CstatusRB.Text = this.MojeConst.TestIdx(cname);
        }

        private void CreateConnBtn_Click(object sender, EventArgs e)
        {
            string cname = this.CnameTb.Text;
            string ctype = this.CtypeCb.Text;
            string cstable = this.Csourcetabletb.Text;
            string crtable = this.CrefTabletb.Text;
            string[] csource = this.SorceRb.Text.Split(',');
            string[] cref = this.ReferencingRB.Text.Split(',');
            this.CstatusRB.Text=this.MojeConst.CreateCon(cname, ctype, cstable, csource, crtable, cref);
            this.vypis_index();
            this.vypis_constrain();
        }

        private void DropConnBTN_Click(object sender, EventArgs e)
        {
            string cname = this.CnameTb.Text;
            this.CstatusRB.Text = this.MojeConst.DropCon(cname);
            this.vypis_index();
            this.vypis_constrain();
        }

        private void lindexlb_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void TestTypeBtn_Click(object sender, EventArgs e)
        {
            string cname = this.CnameTb.Text;
            this.CstatusRB.Clear();
            this.CstatusRB.Text = this.MojeConst.TestType(cname);
        }

        private void ShowPkBtn_Click(object sender, EventArgs e)
        {
            string cstable = this.Csourcetabletb.Text;
            this.CstatusRB.Clear();
            this.CstatusRB.Text = this.MojeConst.ShowPK(cstable);
        }

        private void conlb_DoubleClick(object sender, EventArgs e)
        {
            if (this.conlb.SelectedIndex != -1) {
                SysConstraint.Constrain con = (SysConstraint.Constrain)MojeConst.Constrains[this.conlb.SelectedIndex];
               this.CnameTb.Text=con.Name;
               this.CtypeCb.Text=con.Type;
               this.Csourcetabletb.Text=con.SourceTable;
               this.CrefTabletb.Text=con.ReferenceTable;
               this.SorceRb.Clear();
               for (int i = 0; i < con.Source.Count;i++ )
               {
                   string s = (string)con.Source[i]; ;
                   SorceRb.Text += s;
                   if (i != con.Source.Count - 1)
                   {
                       SorceRb.Text += ",";
                   }
               }
               this.ReferencingRB.Clear();
               for (int i = 0; i < con.Reference.Count; i++)
               {
                   string s = (string)con.Reference[i]; ;
                   ReferencingRB.Text += s;
                   if (i != con.Reference.Count - 1)
                   {
                       ReferencingRB.Text += ",";
                   }
               }
            }
        }
    }
}